package main

import (
	"database/sql"
	"fmt"
	"log"

	_ "github.com/lib/pq"
)

const (
	host     = "localhost"
	port     = 5432
	user     = "postgres"
	password = "Tss1433223"
	dbname   = "test"
)

func connectDB() *sql.DB {
	psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
		"password=%s dbname=%s sslmode=disable",
		host, port, user, password, dbname)

	db, err := sql.Open("postgres", psqlInfo)
	if err != nil {
		panic(err)
	}

	err = db.Ping()
	if err != nil {
		panic(err)
	}
	return db
}

func UpdateUser(db *sql.DB) {
	stmt, err := db.Prepare("UPDATE c set cname=$1 WHERE cno=$2")
	if err != nil {
		log.Fatal(err)
	}
	_, err = stmt.Exec("PE", 4)
	if err != nil {
		log.Fatal(err)
	} else {
		fmt.Println("udpate user_tbl success")
	}

}

func main() {
	db := connectDB()
	UpdateUser(db)
}
